<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="公司名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票号码" prop="fphm">
        <el-input
          v-model="queryParams.fphm"
          placeholder="请输入发票号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票代码" prop="fpdm">
        <el-input
          v-model="queryParams.fpdm"
          placeholder="请输入发票代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:invoice:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:invoice:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:invoice:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:invoice:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="invoiceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司名称" align="center" prop="deptName" />

      <el-table-column label="发票类型" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type === '01'">专用发票</span>
          <span v-if="scope.row.type === '02'">普通发票</span>
        </template>
      </el-table-column>
      <el-table-column label="发票号码" align="center" prop="fphm" />
      <el-table-column label="发票代码" align="center" prop="fpdm" />
      <el-table-column label="开票金额" align="center" prop="hjje" />
      <el-table-column label="开票日期" align="center" prop="kprq" />
      <el-table-column label="销方名称" align="center" prop="mc2" />
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:invoice:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:invoice:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改发票对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入公司id" />
        </el-form-item>
        <el-form-item label="纳税人识别号" prop="nsrsbh1">
          <el-input v-model="form.nsrsbh1" placeholder="请输入纳税人识别号" />
        </el-form-item>
        <el-form-item label="纳税人识别号" prop="nsrsbh2">
          <el-input v-model="form.nsrsbh2" placeholder="请输入纳税人识别号" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="复核" prop="fh">
          <el-input v-model="form.fh" placeholder="请输入复核" />
        </el-form-item>
        <el-form-item label="合计金额" prop="hjje">
          <el-input v-model="form.hjje" placeholder="请输入合计金额" />
        </el-form-item>
        <el-form-item label="合计税额" prop="hjse">
          <el-input v-model="form.hjse" placeholder="请输入合计税额" />
        </el-form-item>
        <el-form-item label="价税合计" prop="jshjxx">
          <el-input v-model="form.jshjxx" placeholder="请输入价税合计" />
        </el-form-item>
        <el-form-item label="价税合计" prop="jshjdx">
          <el-input v-model="form.jshjdx" placeholder="请输入价税合计" />
        </el-form-item>
        <el-form-item label="密码区" prop="mmq">
          <el-input
            v-model="form.mmq"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="开票日期" prop="kprq">
          <el-input v-model="form.kprq" placeholder="请输入开票日期" />
        </el-form-item>
        <el-form-item label="开户行及账号" prop="khhjzh1">
          <el-input v-model="form.khhjzh1" placeholder="请输入开户行及账号" />
        </el-form-item>
        <el-form-item label="名称" prop="mc1">
          <el-input v-model="form.mc1" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="地址、电话" prop="dzdh1">
          <el-input v-model="form.dzdh1" placeholder="请输入地址、电话" />
        </el-form-item>
        <el-form-item label="开户行及账号" prop="khhjzh2">
          <el-input v-model="form.khhjzh2" placeholder="请输入开户行及账号" />
        </el-form-item>
        <el-form-item label="收款人" prop="skr">
          <el-input v-model="form.skr" placeholder="请输入收款人" />
        </el-form-item>
        <el-form-item label="开票人" prop="kpr">
          <el-input v-model="form.kpr" placeholder="请输入开票人" />
        </el-form-item>
        <el-form-item label="名称" prop="mc2">
          <el-input v-model="form.mc2" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="地址、电话" prop="dzdh2">
          <el-input v-model="form.dzdh2" placeholder="请输入地址、电话" />
        </el-form-item>
        <el-form-item label="发票代码" prop="fpdm">
          <el-input v-model="form.fpdm" placeholder="请输入发票代码" />
        </el-form-item>
        <el-form-item label="机器编号" prop="jqbh">
          <el-input v-model="form.jqbh" placeholder="请输入机器编号" />
        </el-form-item>
        <el-form-item label="发票号码" prop="fphm">
          <el-input v-model="form.fphm" placeholder="请输入发票号码" />
        </el-form-item>
        <el-form-item label="校验码" prop="jym">
          <el-input v-model="form.jym" placeholder="请输入校验码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInvoice,
  getInvoice,
  delInvoice,
  addInvoice,
  updateInvoice,
} from "@/api/business/invoice";

export default {
  name: "Invoice",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票表格数据
      invoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        nsrsbh1: null,
        nsrsbh2: null,
        title: null,
        type: null,
        fh: null,
        hjje: null,
        hjse: null,
        jshjxx: null,
        jshjdx: null,
        mmq: null,
        kprq: null,
        khhjzh1: null,
        mc1: null,
        dzdh1: null,
        khhjzh2: null,
        skr: null,
        kpr: null,
        mc2: null,
        dzdh2: null,
        fpdm: null,
        jqbh: null,
        fphm: null,
        jym: null,
        remark: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "id,自增不能为空", trigger: "blur" }],
        deptId: [
          { required: true, message: "公司id不能为空", trigger: "blur" },
        ],
        nsrsbh1: [
          { required: true, message: "纳税人识别号不能为空", trigger: "blur" },
        ],
        nsrsbh2: [
          { required: true, message: "纳税人识别号不能为空", trigger: "blur" },
        ],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        type: [
          { required: true, message: "发票类型不能为空", trigger: "change" },
        ],
        fh: [{ required: true, message: "复核不能为空", trigger: "blur" }],
        hjje: [
          { required: true, message: "合计金额不能为空", trigger: "blur" },
        ],
        hjse: [
          { required: true, message: "合计税额不能为空", trigger: "blur" },
        ],
        jshjxx: [
          { required: true, message: "价税合计不能为空", trigger: "blur" },
        ],
        jshjdx: [
          { required: true, message: "价税合计不能为空", trigger: "blur" },
        ],
        mmq: [{ required: true, message: "密码区不能为空", trigger: "blur" }],
        kprq: [
          { required: true, message: "开票日期不能为空", trigger: "blur" },
        ],
        khhjzh1: [
          { required: true, message: "开户行及账号不能为空", trigger: "blur" },
        ],
        mc1: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        dzdh1: [
          { required: true, message: "地址、电话不能为空", trigger: "blur" },
        ],
        khhjzh2: [
          { required: true, message: "开户行及账号不能为空", trigger: "blur" },
        ],
        skr: [{ required: true, message: "收款人不能为空", trigger: "blur" }],
        kpr: [{ required: true, message: "开票人不能为空", trigger: "blur" }],
        mc2: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        dzdh2: [
          { required: true, message: "地址、电话不能为空", trigger: "blur" },
        ],
        fpdm: [
          { required: true, message: "发票代码不能为空", trigger: "blur" },
        ],
        jqbh: [
          { required: true, message: "机器编号不能为空", trigger: "blur" },
        ],
        fphm: [
          { required: true, message: "发票号码不能为空", trigger: "blur" },
        ],
        jym: [{ required: true, message: "校验码不能为空", trigger: "blur" }],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询发票列表 */
    getList() {
      this.loading = true;
      listInvoice(this.queryParams).then((response) => {
        this.invoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        nsrsbh1: null,
        nsrsbh2: null,
        title: null,
        type: null,
        fh: null,
        hjje: null,
        hjse: null,
        jshjxx: null,
        jshjdx: null,
        mmq: null,
        kprq: null,
        khhjzh1: null,
        mc1: null,
        dzdh1: null,
        khhjzh2: null,
        skr: null,
        kpr: null,
        mc2: null,
        dzdh2: null,
        fpdm: null,
        jqbh: null,
        fphm: null,
        jym: null,
        remark: null,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发票";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInvoice(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改发票";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInvoice(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvoice(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除发票编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInvoice(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/invoice/export",
        {
          ...this.queryParams,
        },
        `invoice_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
